const {Router} = require('express')
const router = new Router()

const path = require('path')
// 引入轮播图模型
const RotationModel = require('../../../model/rotation')
// 引入url
const url = require('../../../config/url')

router.get('/public/charging/*',function (request, response) {
    const fileName = (path.parse(request.path)).base
    const rotationPic = `${url}public/charging/rotation/${fileName}`

    // 计费轮播图只针对8080端口下显示计费
    if (request.get('Referer') == 'http://localhost:8080/' || request.get('idea') == 'fantasy'){
        RotationModel.updateOne({
            rotationPic
        },{
            $inc:{ chargeNum: -1 }
        },function (err,doc) {
            // 将投标数为0的变更为不展示
            RotationModel.updateMany({
                isShow: 0,
                chargeNum: 0
            },{
                isShow: 1
            })
            // 当存库发生错误时，给前端发送err图片
            if (err) return response.sendFile(__dirname,'../../../public/free/picErr.jpg');
            response.sendFile(path.resolve(__dirname,'../../../public/charging/rotation',fileName))
        })

    }else {
        response.sendFile(path.resolve(__dirname,'../../../public/charging/rotation',fileName))
    }

})

module.exports = router
